package com.twf.usercenter.common.utils;

import java.lang.reflect.Field;
@Deprecated//弃用
public class BaseObject {
    public static <T, V> V copy(T domain, Class<V> clazz){
        if (domain==null) return null;
        V res = null;
        try {
            res = clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Field[] domainFields = domain.getClass().getDeclaredFields();
        Field[] resFields = clazz.getDeclaredFields();

        for (Field domainField : domainFields) {
            if (domainField.getName().equals("serialVersionUID")){
                continue;
            }
            for (Field resField : resFields) {
                if (domainField.getName().equals(resField.getName()) && domainField.getType().equals(resField.getType())) {
                    domainField.setAccessible(true);
                    resField.setAccessible(true);
                    try {
                        resField.set(res, domainField.get(domain));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        return res;
    }
}
